package com.example.demo.controller;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.constructor.DuplicateKeyException;

import com.example.demo.common.Result;
import com.example.demo.pojo.Page;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;






@RestController
@RequestMapping("/user")
public class UserController {
    

    @Autowired
    private UserService userService;
    
    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        try {
            userService.insert(user);
        } catch (Exception e) {
            if(e instanceof DuplicateKeyException)
                return Result.error("数据库插入错误");
            else
                return Result.error("未知错误");
        }
        return Result.success();
    }

    @PutMapping("/update")
    public Result update( @RequestBody User user) {

        userService.updateUser(user);
        return Result.success();
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        userService.deleteUser(id);
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        userService.deleteBatchUser(ids);
        return Result.success();
    }
    @GetMapping("/selectAll")
    public Result selectALL() {
        List<User> users = userService.selectAll();
        return Result.success(users);
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        User user = userService.selectById(id);
        return Result.success(user);
    }
    @GetMapping("/selectByName/{name}")
    public Result selectByName(@PathVariable String name) {
        List<User> users = userService.selectByName(name);
        return Result.success(users);
    }
    
    @GetMapping("/selectByMore")
    public Result selectByMore(@RequestParam String username, @RequestParam String name) {
        List<User> users = userService.selectByMore(username, name);
        return Result.success(users);
    }

    @GetMapping("/selectByLike")
    public Result selectByLike(@RequestParam String username, @RequestParam String name) {
        List<User> users = userService.selectByLike(username, name);
        return Result.success(users);

    }

    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                              @RequestParam Integer pageSize,
                              @RequestParam String username,
                              @RequestParam String name) {
       Page<User> page = userService.selectByPage(pageNum, pageSize, username, name);
        return Result.success(page);
    }
    
    
    
    
}
